package com.inno.subject.controller.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.google.common.base.Preconditions;
import com.inno.subject.common.context.LoginUserContext;
import com.inno.subject.common.entity.CommonReturnType;
import com.inno.subject.common.entity.PageResult;
import com.inno.subject.controller.convert.SubjectInfoDTOConverter;
import com.inno.subject.controller.dto.CategorySubjectCountDTO;
import com.inno.subject.controller.dto.SubjectInfoDTO;
import com.inno.subject.domain.entity.CategorySubjectCountBO;
import com.inno.subject.domain.entity.SubjectInfoBO;
import com.inno.subject.domain.service.SubjectInfoDomainService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author sean
 */
@Slf4j
@RestController
@RequestMapping("/subject/info")
@RequiredArgsConstructor
public class SubjectInfoController {
    private final SubjectInfoDomainService subjectInfoDomainService;

    /**
     * 每日一练
     */
    @PostMapping("/daily")
    public CommonReturnType<List<SubjectInfoDTO>> daily() {
        SubjectInfoBO subjectInfoBO = new SubjectInfoBO();
        subjectInfoBO.setCurrentUser(LoginUserContext.getUserId());
        List<SubjectInfoBO> boList = this.subjectInfoDomainService.daily(subjectInfoBO);

        return CommonReturnType.success(SubjectInfoDTOConverter.INSTANCE.convertBOListToDTOList(boList));
    }

    /**
     * 新增题目
     */
    @PostMapping("/add")
    public CommonReturnType<Void> add(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(), "分类不能为空");
        Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(), "题目类型不能为空");
        if (subjectInfoDTO.getSubjectType() == 1) {
            Preconditions.checkArgument(!StringUtils.isBlank(subjectInfoDTO.getSubjectAnswer()), "答案不能为空");
        }
        Preconditions.checkArgument(!StringUtils.isBlank(subjectInfoDTO.getSubjectName()), "题目名称不能为空");
        Preconditions.checkArgument(!StringUtils.isBlank(subjectInfoDTO.getSubjectLevel()), "题目难度不能为空");
        subjectInfoDTO.setCurrentUser(LoginUserContext.getUserId());
        SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);
        this.subjectInfoDomainService.add(subjectInfoBO);
        return CommonReturnType.success();
    }

    /**
     * 题目列表
     */
    @PostMapping("/queryPage")
    public CommonReturnType<PageResult<SubjectInfoDTO>> queryPage(@RequestBody SubjectInfoDTO subjectInfoDTO) {
//        Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(), "分类不能为空");
        Preconditions.checkNotNull(subjectInfoDTO.getPage(), "分页参数不能为空");
        Preconditions.checkNotNull(subjectInfoDTO.getPageSize(), "分页参数不能为空");
        if (subjectInfoDTO.getCategoryId() == null) {
            subjectInfoDTO.setCategoryId(0L);
        }
        if (subjectInfoDTO.getCategoryId() == 0) {
            subjectInfoDTO.setCategoryId(null);
        }
        subjectInfoDTO.setCurrentUser(LoginUserContext.getUserId());
        SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);
        PageResult<SubjectInfoBO> boResult = this.subjectInfoDomainService.queryPage(subjectInfoBO);
        List<SubjectInfoDTO> dtoList = SubjectInfoDTOConverter.INSTANCE.convertBOListToDTOList(boResult.getList());

        return CommonReturnType.success(PageResult.of(dtoList, boResult.getTotal()));
    }

    /**
     * 题目详情
     */
    @PostMapping("/queryById")
    public CommonReturnType<SubjectInfoDTO> queryById(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        Preconditions.checkNotNull(subjectInfoDTO.getId(), "题目id不能为空");
        subjectInfoDTO.setCurrentUser(LoginUserContext.getUserId());
        SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);
        SubjectInfoBO boResult = this.subjectInfoDomainService.queryById(subjectInfoBO);
        SubjectInfoDTO dtoResult = SubjectInfoDTOConverter.INSTANCE.convertBOToDTO(boResult);
        return CommonReturnType.success(dtoResult);
    }

    /**
     * 修改题目
     */
    @PostMapping("/update")
    public CommonReturnType<Void> update(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        Preconditions.checkNotNull(subjectInfoDTO.getId(), "题目id不能为空");
        SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);
        this.subjectInfoDomainService.update(subjectInfoBO);
        return CommonReturnType.success();
    }

    /**
     * 删除题目
     */
    @PostMapping("/delete")
    public CommonReturnType<Void> delete(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        if (CollectionUtil.isEmpty(subjectInfoDTO.getIds())){
            throw new IllegalArgumentException("id不能为空");
        }
        this.subjectInfoDomainService.delete(subjectInfoDTO.getIds());
        return CommonReturnType.success();
    }
    /**
     * 错题列表
     */
    @PostMapping("/queryErrorSubject")
    public CommonReturnType<PageResult<SubjectInfoDTO>> queryErrorSubject(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        Preconditions.checkNotNull(subjectInfoDTO.getPage(), "分页参数不能为空");
        Preconditions.checkNotNull(subjectInfoDTO.getPageSize(), "分页参数不能为空");
        subjectInfoDTO.setCurrentUser(LoginUserContext.getUserId());
        SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);
        PageResult<SubjectInfoBO> boResult = this.subjectInfoDomainService.queryErrorSubject(subjectInfoBO);
        List<SubjectInfoDTO> dtoList = SubjectInfoDTOConverter.INSTANCE.convertBOListToDTOList(boResult.getList());

        return CommonReturnType.success(PageResult.of(dtoList, boResult.getTotal()));
    }

    @PostMapping("/totalCount")
    public CommonReturnType<Long> totalCount() {
        return CommonReturnType.success(this.subjectInfoDomainService.queryCount());
    }

    @GetMapping("/categorySubjectCount")
    public CommonReturnType<List<CategorySubjectCountDTO>> categorySubjectCount() {
        List<CategorySubjectCountBO> boResult = this.subjectInfoDomainService.categorySubjectCount();
        List<CategorySubjectCountDTO> categorySubjectCountDTOS = BeanUtil.copyToList(boResult, CategorySubjectCountDTO.class);
        return CommonReturnType.success(categorySubjectCountDTOS);
    }
}
